package com.fjwt.gz.task.activity;

import com.fjwt.gz.db.entity.StatActivityNumEntity;
import com.fjwt.gz.service.impl.StatActivityNumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 活动统计--数量
 */
@Component
@Slf4j
public class ActivityStatisticsTask {

    @Autowired
    private StatActivityNumService statActivityNumService;

//    @Scheduled(cron = "* * * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void activityStatisticsNum() {

        try {
            List<StatActivityNumEntity> list = null;
            // 在Service中使用
            boolean hasData = statActivityNumService.exists(null);
            if (hasData) {
                // 表中有数据
                list = statActivityNumService.activityStatisticsList(1);
            } else {
                // 表中无数据
                list = statActivityNumService.activityStatisticsList(0);
            }

            if (list != null && !list.isEmpty()) {
                log.info("开始批量插入活动统计数据，共 {} 条记录", list.size());

                // 批量插入数据
                boolean result = statActivityNumService.saveBatch(list);

                if (result) {
                    log.info("批量插入活动统计数据成功，共 {} 条记录", list.size());
                } else {
                    log.error("批量插入活动统计数据失败");
                }
            } else {
                log.info("没有需要插入的活动统计数据");
            }
        } catch (Exception e) {
            log.error("活动统计数据统计任务执行异常", e);
            throw e;
        }
    }

}
